﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Schedule;
using WomanMotion.WebApi.Model.Dtos.Team;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 赛程管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("赛程管理", true, 60)]
    public class ScheduleController : BaseController
    {
        private readonly IScheduleService _scheduleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="scheduleService"></param>
        public ScheduleController(ILogger<ScheduleController> logger, IScheduleService scheduleService) : base(logger)
        {
            _scheduleService = scheduleService;
        }

        /// <summary>
        /// 创建赛程
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ScheduleVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建赛程", true, "赛程管理", "List", "ScheduleList")]
        public async Task<IResultOutput> Created(CreatedScheduleDTO dto)
        {
            var res = await _scheduleService.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用赛程
        /// </summary>
        /// <param name="ids">ID主键数组</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用赛程")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _scheduleService.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用赛程
        /// </summary>
        /// <param name="ids">ID主键数组</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用赛程")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _scheduleService.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新赛程
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ScheduleVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新赛程")]
        public async Task<IResultOutput> Update(UpdateScheduleDTO dto)
        {
            var res = await _scheduleService.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取赛程信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ScheduleVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取赛程信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _scheduleService.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接赛程信息
        /// </summary>
        /// <param name="id">id主键</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<ScheduleVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接赛程信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _scheduleService.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取赛程数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<ScheduleVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取赛程数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _scheduleService.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取赛程关联比赛的数据
        /// </summary>
        /// <param name="pars"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<ScheduleAndMatchPageVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取赛程关联比赛的数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetScheduleAndMatchPageList(QueryParameterBase pars)
        {
            var res = await _scheduleService.GetScheduleAndWatchPageListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}